package com.woniu.fly.fairmall.db.service;

import com.github.pagehelper.PageHelper;
import com.woniu.fly.fairmall.db.dao.FairmallUserDao;
import com.woniu.fly.fairmall.db.entity.FairmallUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户表(FairmallUser)表服务实现类
 *
 * @author makejava
 * @since 2020-03-19 14:23:08
 */
@Service("fairmallUserService")
public class FairmallUserService {
    @Resource
    private FairmallUserDao fairmallUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public FairmallUser queryById(Long id) {
        return this.fairmallUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    public List<FairmallUser> queryAllByLimit(int offset, int limit) {
        return this.fairmallUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param fairmallUser 实例对象
     * @return 实例对象
     */
    public FairmallUser insert(FairmallUser fairmallUser) {
        fairmallUser.setAddTime(LocalDateTime.now());
        fairmallUser.setUpdateTime(LocalDateTime.now());
        this.fairmallUserDao.insert(fairmallUser);
        return fairmallUser;
    }

    /**
     * 修改数据
     *
     * @param fairmallUser 实例对象
     * @return 实例对象
     */
    public int update(FairmallUser fairmallUser) {
        fairmallUser.setUpdateTime(LocalDateTime.now());
        return this.fairmallUserDao.update(fairmallUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Long id) {
        return this.fairmallUserDao.deleteById(id) > 0;
    }

    /**
     * 查询用户数
     *
     * @return 数量
     */
    public long count() {
        return this.fairmallUserDao.count();
    }

    public List<FairmallUser> queryAllListByPage(String username, String mobile, Integer page, Integer limit) {
        FairmallUser user = new FairmallUser();
        user.setUsername(username);
        user.setMobile(mobile);
        PageHelper.startPage(page, limit);
        return fairmallUserDao.queryAll(user);
    }

    public List<FairmallUser> queryByUsername(String username) {
        FairmallUser user = new FairmallUser();
        user.setUsername(username);
        return fairmallUserDao.queryAll(user);
    }

    public FairmallUser queryByOid(String openId) {
        return fairmallUserDao.queryByOid(openId);
    }

    public List<FairmallUser> queryByMobile(String mobile) {
        FairmallUser user = new FairmallUser();
        user.setMobile(mobile);
        return fairmallUserDao.queryAll(user);
    }
}
